package com.jz.netthings.engine.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


//@WebFilter(urlPatterns = "/*",filterName = "channelFilter") //注册过滤器
public class jzMachineRequestFilter implements Filter {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("jzMachineRequestFilter 初始化.......");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;

        String reqUrl = httpRequest.getRequestURI();
        if (reqUrl.endsWith("/")) {
            reqUrl = reqUrl.substring(0, reqUrl.length() - 1);
        }
        logger.info("请求:  " + reqUrl);

        String reqUrl_path = "";
        String reqUrl_data = "";
        {
            int p = reqUrl.lastIndexOf('/');
            if (p > 0) {
                reqUrl_path = reqUrl.substring(0, p);
                reqUrl_data = reqUrl.substring(p + 1);
            }
        }

        int execmode = -1;
        while (true) {
            //平板读取单采机发送消息的接口
            if (reqUrl_path.endsWith("/mail")) {
                execmode = 1;
                break;
            }

            //护士信息
            if (reqUrl.endsWith("/users")) {
                execmode = 2;
                break;
            }

            //护士个人信息
            if (reqUrl_path.endsWith("/users")) {
                execmode = 3;
                break;
            }

            //浆员信息
            if (reqUrl_path.endsWith("/donors")) {
                execmode = 4;
                break;
            }

            //浆员献浆记录
            if (reqUrl.endsWith("/apheresisLogs")) {
                execmode = 5;
                break;
            }

            //耗材批次
            if (reqUrl.endsWith("/materials")) {
                execmode = 6;
                break;
            }

            //检查浆员能否献浆信息
            if (reqUrl_path.endsWith("/checkdonors")) {
                execmode = 7;
                break;
            }

            //通用码表服务
            if (reqUrl_path.endsWith("/generalCodes")) {
                execmode = 8;
                break;
            }

            //获取指定护士管理的献浆员今天的进站情况
            if (reqUrl_path.endsWith("/nurseComedProviders")) {
                execmode = 9;
                break;
            }

            break;
        }

        httpRequest.setCharacterEncoding("UTF-8");
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        httpResponse.setContentType("text/plain; charset=utf-8");

        httpResponse.getWriter().print("lllllllllllllllllllll");
        httpResponse.flushBuffer();

    }

    @Override
    public void destroy() {

    }
}
